//
//  CacheManager.m
//  OneCard
//
//  Created by sally on 16/2/24.
//  Copyright © 2016年 koalac. All rights reserved.
//

#import "CacheManager.h"
#import "LevelModel.h"
#import "MainModel.h"
#import "OtherModel.h"
#import "PlistManager.h"

static CacheManager *_instance = nil;

@implementation CacheManager

+ (CacheManager *)shareInstance {
  if (!_instance) {
    @synchronized(self) {
      if (!_instance) {
        _instance = [[self alloc] init];
      }
    }
  }
  return _instance;
}

- (BOOL)cacheLastLoginAccount:(UserModel *)accout {
  if (accout) {
    NSString *plistIdentify = PLIST_FILE_LAST_LOGIN_ACCOUNT;
    NSMutableDictionary *savedMDict = [self getSavedDataFromPlistByIdentify:plistIdentify];
    [savedMDict removeAllObjects];
    NSDictionary *dict = [UserModel getObjectData:accout];
    [savedMDict addEntriesFromDictionary:dict];

    return [[PlistManager shareInstance] saveToPlist:plistIdentify andDictionary:savedMDict];
  } else {
    return NO;
  }
}

- (UserModel *)getLastLoginAccount {
  UserModel *account = nil;
  NSString *plistIdentify = PLIST_FILE_LAST_LOGIN_ACCOUNT;
  if ([[PlistManager shareInstance] isPlistExists:plistIdentify]) {
    NSMutableDictionary *savedMDict = [self getSavedDataFromPlistByIdentify:plistIdentify];
    if (savedMDict.count > 0) {
      account = [[UserModel alloc] initWithDictionary:savedMDict error:nil];
      //FIXME: 为什么，注意uid这种转换过的名称需要额外转换。
      account.uid = [[savedMDict objectForKey:@"uid"] integerValue];
    }
  }
  return account;
}

- (BOOL)cacheLastLevelArr:(NSMutableArray *)levelArr {
  if (levelArr) {
    NSString *plistIdentify = PLIST_FILE_LAST_LEVEL1_ARR;
    NSMutableDictionary *savedMDict = [self getSavedDataFromPlistByIdentify:plistIdentify];
    [savedMDict removeAllObjects];
    NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    for (LevelModel *levelModel in levelArr) {
      NSDictionary *dict = [LevelModel getObjectData:levelModel];
      [resultArr addObject:dict];
    }
    return [[PlistManager shareInstance] saveToPlist:plistIdentify andArray:resultArr];
  } else {
    return NO;
  }
}

- (NSMutableArray *)getLastLevelArr {
  NSMutableArray *levelArr = [NSMutableArray array];
  NSString *plistIdentify = PLIST_FILE_LAST_LEVEL1_ARR;
  if ([[PlistManager shareInstance] isPlistExists:plistIdentify]) {
    NSMutableArray *savedMArr = [self getArraySavedDataFromPlistByIdentify:plistIdentify];
    if (savedMArr.count > 0) {
      for (id item in savedMArr) {
        LevelModel *levelModel = [[LevelModel alloc] initWithDictionary:item error:nil];
        levelModel.levelid = [[item objectForKey:@"levelid"] integerValue];
        [levelArr addObject:levelModel];
      }
    }
  }
  return levelArr;
}

- (BOOL)cacheLastLevel2Arr:(NSMutableArray *)levelArr identify:(NSString *)plistIdentify {
  if (levelArr) {
    NSMutableDictionary *savedMDict = [self getSavedDataFromPlistByIdentify:plistIdentify];
    [savedMDict removeAllObjects];
    NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    for (LevelModel *levelModel in levelArr) {
      NSDictionary *dict = [LevelModel getObjectData:levelModel];
      [resultArr addObject:dict];
    }
    return [[PlistManager shareInstance] saveToPlist:plistIdentify andArray:resultArr];
  } else {
    return NO;
  }
}

- (NSMutableArray *)getLastLevelArrWithIdentify:(NSString *)plistIdentify {
  NSMutableArray *levelArr = [NSMutableArray array];
  if ([[PlistManager shareInstance] isPlistExists:plistIdentify]) {
    NSMutableArray *savedMArr = [self getArraySavedDataFromPlistByIdentify:plistIdentify];
    if (savedMArr.count > 0) {
      for (id item in savedMArr) {
        LevelModel *levelModel = [[LevelModel alloc] initWithDictionary:item error:nil];
        levelModel.levelid = [[item objectForKey:@"levelid"] integerValue];
        [levelArr addObject:levelModel];
      }
    }
  }
  return levelArr;
}

#pragma mark 获取PLIST文件中已保存的数据
- (NSMutableDictionary *)getSavedDataFromPlistByIdentify:(NSString *)plistIdentify {
  NSMutableDictionary *savedMDict = [NSMutableDictionary dictionary];
  if ([[PlistManager shareInstance] isPlistExists:plistIdentify]) {
    savedMDict = [[PlistManager shareInstance] getSavedInPlist:plistIdentify];
  }
  return savedMDict;
}

- (NSMutableArray *)getArraySavedDataFromPlistByIdentify:(NSString *)plistIdentify {
  NSMutableArray *savedMArr = [NSMutableArray array];
  if ([[PlistManager shareInstance] isPlistExists:plistIdentify]) {
    savedMArr = [[PlistManager shareInstance] getArraySavedInPlist:plistIdentify];
  }
  return savedMArr;
}

- (BOOL)cacheLocArr:(NSMutableArray *)locArr {
  if (locArr) {
    NSString *plistIdentify = PLIST_FILE_LAST_LOC_ARR;
    NSMutableDictionary *savedMDict = [self getSavedDataFromPlistByIdentify:plistIdentify];
    [savedMDict removeAllObjects];
    NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    for (LocationModel *locModel in locArr) {
      NSDictionary *dict = [LocationModel getObjectData:locModel];
      [resultArr addObject:dict];
    }
    return [[PlistManager shareInstance] saveToPlist:plistIdentify andArray:resultArr];
  } else {
    return NO;
  }
}

- (NSMutableArray *)getLocArr {
  NSMutableArray *locArr = [NSMutableArray array];
  NSString *plistIdentify = PLIST_FILE_LAST_LOC_ARR;
  if ([[PlistManager shareInstance] isPlistExists:plistIdentify]) {
    NSMutableArray *savedMArr = [self getArraySavedDataFromPlistByIdentify:plistIdentify];
    if (savedMArr.count > 0) {
      for (id item in savedMArr) {
        LocationModel *locModel = [[LocationModel alloc] initWithDictionary:item error:nil];
        locModel.locid = [[item objectForKey:@"locid"] integerValue];
        [locArr addObject:locModel];
      }
    }
  }
  return locArr;
}

- (BOOL)cacheYearArr:(NSMutableArray *)yearArr {
  if (yearArr) {
    NSString *plistIdentify = PLIST_FILE_LAST_YEAR_ARR;
    NSMutableDictionary *savedMDict = [self getSavedDataFromPlistByIdentify:plistIdentify];
    [savedMDict removeAllObjects];
    NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    for (TimeModel *timeModle in yearArr) {
      NSDictionary *dict = [TimeModel getObjectData:timeModle];
      [resultArr addObject:dict];
    }
    return [[PlistManager shareInstance] saveToPlist:plistIdentify andArray:resultArr];
  } else {
    return NO;
  }
}

- (NSMutableArray *)getYearArr {
  NSMutableArray *yearArr = [NSMutableArray array];
  NSString *plistIdentify = PLIST_FILE_LAST_YEAR_ARR;
  if ([[PlistManager shareInstance] isPlistExists:plistIdentify]) {
    NSMutableArray *savedMArr = [self getArraySavedDataFromPlistByIdentify:plistIdentify];
    if (savedMArr.count > 0) {
      for (id item in savedMArr) {
        TimeModel *timeModle = [[TimeModel alloc] initWithDictionary:item error:nil];
        timeModle.timeid = [[item objectForKey:@"timeid"] integerValue];
        [yearArr addObject:timeModle];
      }
    }
  }
  return yearArr;
}

- (BOOL)cacheEduArr:(NSMutableArray *)eduArr {
  if (eduArr) {
    NSString *plistIdentify = PLIST_FILE_LAST_EDU_ARR;
    NSMutableDictionary *savedMDict = [self getSavedDataFromPlistByIdentify:plistIdentify];
    [savedMDict removeAllObjects];
    NSMutableArray *resultArr = [[NSMutableArray alloc] init];
    for (TimeModel *timeModle in eduArr) {
      NSDictionary *dict = [TimeModel getObjectData:timeModle];
      [resultArr addObject:dict];
    }
    return [[PlistManager shareInstance] saveToPlist:plistIdentify andArray:resultArr];
  } else {
    return NO;
  }
}

- (NSMutableArray *)getEduArr {
  NSMutableArray *eduArr = [NSMutableArray array];
  NSString *plistIdentify = PLIST_FILE_LAST_EDU_ARR;
  if ([[PlistManager shareInstance] isPlistExists:plistIdentify]) {
    NSMutableArray *savedMArr = [self getArraySavedDataFromPlistByIdentify:plistIdentify];
    if (savedMArr.count > 0) {
      for (id item in savedMArr) {
        TimeModel *timeModle = [[TimeModel alloc] initWithDictionary:item error:nil];
        timeModle.timeid = [[item objectForKey:@"timeid"] integerValue];
        [eduArr addObject:timeModle];
      }
    }
  }
  return eduArr;
}

- (BOOL)cacheLastBecomeExpert:(BecomeExpertModel *)becomeExpert {
  if (becomeExpert) {
    NSString *plistIdentify = PLIST_FILE_LAST_BECOME_EXPERT;
    NSMutableDictionary *savedMDict = [self getSavedDataFromPlistByIdentify:plistIdentify];
    [savedMDict removeAllObjects];
    NSDictionary *dict = [BecomeExpertModel getObjectData:becomeExpert];
    [savedMDict addEntriesFromDictionary:dict];

    return [[PlistManager shareInstance] saveToPlist:plistIdentify andDictionary:savedMDict];
  } else {
    return NO;
  }
}

- (BecomeExpertModel *)getBecomeExpert {
  BecomeExpertModel *becomeExpert = nil;
  NSString *plistIdentify = PLIST_FILE_LAST_BECOME_EXPERT;
  if ([[PlistManager shareInstance] isPlistExists:plistIdentify]) {
    NSMutableDictionary *savedMDict = [self getSavedDataFromPlistByIdentify:plistIdentify];
    if (savedMDict.count > 0) {
      becomeExpert = [[BecomeExpertModel alloc] initWithDictionary:savedMDict error:nil];
      becomeExpert.uid = [[savedMDict objectForKey:@"uid"] integerValue];
    }
  }
  return becomeExpert;
}

@end
